const getRawBody = require('raw-body');
const contentType = require('content-type');
const { parseString } = require('xml2js');
module.exports = {
    /**
     * 用rawbody来解析xml流数据
     * @param {} req 
     */
    getXmlData(req) {
        return new Promise((resolve, reject) => {
            getRawBody(req, {
                length: req.headers['content-length'],
                limit: '1mb',
                encoding: contentType.parse(req).parameters.charset
            }).then(res => {
                resolve(res.toString());
            })

        });

    },
    getXMlJson(xml) {
        return new Promise((resolve, reject) => {
            parseString(xml, function (err, result) {
                resolve(result);
            });

        });

    },
    /**格式化微信的数据格式 */
    formatWxJson(data) {
        let newArr = {};
        for (const key in data) {
            const value = data[key];
            if (Array.isArray(value) && value.length > 0) {
                newArr[key] = value[0];
            }
        }
        return newArr;
    }

}